//
//  SearchYellowPaygVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/15.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "SearchYellowPaygVC.h"
#import "YellowPageCell.h"
#import "YellowPageListModel.h"
#import "EmploymentSearch.h"
#import "YellowPageNothingCell.h"

@interface SearchYellowPaygVC ()
@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;

@property (nonatomic, assign) NSString *totalCount;

@property (nonatomic, strong) NSMutableArray<YellowPageListModel *> *dataModels;


@end

@implementation SearchYellowPaygVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"企业黄页";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.plainTableView.backgroundColor = viewBgGrey;
    
    
    self.plainTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.plainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.plainTableView.estimatedRowHeight = 100;
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([YellowPageCell class]) bundle:nil] forCellReuseIdentifier:[YellowPageCell identifierStr]];
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EmploymentSearch class]) bundle:nil] forHeaderFooterViewReuseIdentifier:[EmploymentSearch identifierStr]];
    
    [self.plainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([YellowPageNothingCell class]) bundle:nil] forCellReuseIdentifier:[YellowPageNothingCell identifierStr]];
    
    
    MJWeakSelf
    self.plainTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.plainTableView.mj_header beginRefreshing];
    
    
    
    
    
    // Do any additional setup after loading the view.
}



- (void)loadData{
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"page"] = @"1";
    parameters[@"pagesize"] = @"20";
    parameters[@"keyword"] = self.keyword;

    //    keyword
    
    //    parameters[@"uid"] = myViewModel.user.uid;
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Recruit/getYellowPages" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //                                    MYJSONSTR
            
            //                    [[responseObject[@"data"][@"data"] firstObject]  propertyCode];
            
            weakSelf.pageCount = responseObject[@"data"][@"page_count"];
            
            weakSelf.totalCount = responseObject[@"data"][@"total_count"];
            
            
            weakSelf.dataModels = [YellowPageListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            
            
            //                        [weakSelf.dataModels removeAllObjects];
            //
            //
            //                        weakSelf.totalCount = @"0";
            //
            //            NSLog(@"%ld",weakSelf.dataModels.count);
            
            if ([weakSelf.totalCount isEqualToString:@"0"]) {
                
                
                
            } else {
                
                weakSelf.plainTableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
                    
                    [weakSelf loadMoreData];
                    
                }];
                
                [weakSelf.plainTableView.mj_footer resetNoMoreData];
                
                
                if ([weakSelf.pageCount isEqualToString:@"1"]) {
                    
                    [weakSelf.plainTableView.mj_footer endRefreshingWithNoMoreData];
                    
                }
            }
            
            [weakSelf.plainTableView.mj_header endRefreshing];
            
            
            
            [weakSelf.plainTableView reloadData];
            
            
        } else {
            
            [weakSelf.plainTableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
}



- (void)loadMoreData{
    
    self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pagesize"] = @"20";
    parameters[@"page"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    parameters[@"keyword"] = self.keyword;

    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"Recruit/getYellowPages" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            NSMutableArray<YellowPageListModel *> *temp = [YellowPageListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
            
            
            
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            
            
            
            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                [weakSelf.plainTableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.plainTableView.mj_footer endRefreshing];
                
                
            }
            
            
            [weakSelf.plainTableView reloadData];
            
        }else{
            
            [weakSelf.plainTableView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
    
    
}





#pragma mark - tableviewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModels == nil) {
        return  0;
    } else {
        return  1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.dataModels == nil) {
        return  0;
    } else {
        
        if (self.dataModels.count == 0) {
            return 1;
        } else {
            return  self.dataModels.count;
        }
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataModels.count == 0) {
        
        
        YellowPageNothingCell *cell = [tableView dequeueReusableCellWithIdentifier:[YellowPageNothingCell identifierStr]];
        
        
        return cell;
        
        
    } else {
        
        
        YellowPageCell *cell = [tableView dequeueReusableCellWithIdentifier:[YellowPageCell identifierStr]];
        
        cell.model = self.dataModels[indexPath.row];
        
        
        return cell;
        
        
    }
    
    
    
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    if (self.dataModels.count == 0) {
//        CGFloat tempH = IPhoneX ? 88 : 64;
//        return KScreenH - tempH;
//
//    } else {
//
//        return 170;
//
//    }
//
//}



//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//
//    return 0.00001;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//
//    if (self.dataModels == nil || self.dataModels.count == 0) {
//        return 0.00001;
//    } else {
//        return 52;
//    }
//
//
//
//
//
//
//
//
//}



//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    
//    
//    UIView *temp = [[UIView alloc]init];
//    temp.backgroundColor = [UIColor whiteColor];
//    return temp;
//}
//
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    
//    
//    if (self.dataModels == nil || self.dataModels.count == 0) {
//        
//        UIView *temp = [[UIView alloc]init];
//        temp.backgroundColor = [UIColor whiteColor];
//        return temp;
//        
//    }else{
//        
//        EmploymentSearch *view = (EmploymentSearch *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:[EmploymentSearch identifierStr]];
//        
//        return view;
//    }
//    
//    
//    
//    
//    
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
